package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 工作台展示代接，待派送，已完成，已取消，全部订单数
     */
    @Override
    public OrderOverViewVO showCountOrder() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        //给vo数据赋值
        orderOverViewVO.setAllOrders(orderMapper.countAll());
        orderOverViewVO.setDeliveredOrders(orderMapper.countByStatus(Orders.CONFIRMED));
        orderOverViewVO.setCompletedOrders(orderMapper.countByStatus(Orders.COMPLETED));
        orderOverViewVO.setCancelledOrders(orderMapper.countByStatus(Orders.CANCELLED));
        orderOverViewVO.setWaitingOrders(orderMapper.countByStatus(Orders.TO_BE_CONFIRMED));
        return orderOverViewVO;
    }

    /**
     * 菜品停，启售数量显示
     */
    @Override
    public DishOverViewVO showCountDishs() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 套餐停，启售数量显示
     */
    @Override
    public SetmealOverViewVO showCountSetmeal() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(setmealMapper.countByStatus(StatusConstant.ENABLE));
        setmealOverViewVO.setDiscontinued(setmealMapper.countByStatus(StatusConstant.DISABLE));
        return setmealOverViewVO;
    }

    /**
     * 功能描述: 工作台展示数据
     *
     * @return : com.sky.vo.BusinessDataVO
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.CANCELLED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if (totalOrderCount != 0 && validOrderCount != 0) {
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }
}
